<?php
namespace app\home\controller;
use \think\Cookie;
use \think\View;
use \think\Model;
class User extends Model
{
    protected $resultSetType = 'collection';
}

class StatusData extends Model
{
    protected $resultSetType = 'collection';
    protected $createTime = 'time';
    protected $updateTime = false;
}
class Comment extends Model
{
    protected $resultSetType = 'collection';
    protected $createTime = 'time';
    protected $updateTime = false;
}
class Blog extends Model
{
    protected $resultSetType = 'collection';
    protected $createTime = 'time';
    protected $updateTime = false;
}
class Blogpage extends \think\Controller
{
    public function blog()    //动态程序
    {
        $cookie = Cookie::get('safecode');
        $person = User::getBySafecode($cookie);
        $blog_data = Blog::where('status', 1)->order('time', 'desc')->paginate(3);
        $page = $blog_data->render();
        $view = new View();
        for ($i = 0; $i < count($blog_data); ++$i) {
            $blog_data[$i]['comment'] = Comment::where('blog_Id', $blog_data[$i]['Id'])->order('time', 'desc')->select()->toArray();
        }
        $view->assign('blog', $blog_data);
        $view->assign('person', $person);
        $view->assign('page', $page);
        return $view->fetch('blog', [], ['__PUBLIC__' => '/public/static']);
    }

    public function add()       //发表动态
    {
        $view = new View();
        return $view->fetch('add', [], ['__PUBLIC__' => '/public/static/home']);
    }

    public function upload_image()  //上传图片
    {
        $file = request()->file('file');
        if ($file) {
            $info = $file->move(ROOT_PATH . 'public/images');
            if ($info) {
                $dir = '/public/images/' . $info->getSaveName();
                return json(['dir' => $dir]);
            }
        }
    }

    public function add_blog()  //动态发表接口
    {
        if (input('post.')) {
            $data = input('post.data');
            $cookie = Cookie::get('safecode');
            $status = new StatusData();
            $person = User::getBySafecode($cookie);
            $post_data['name'] = $person['name'];
            $post_data['qq'] = $person['qq'];
            $post_data['info'] = $data;
            $blog = new Blog($post_data);
            if ($blog->save()) {
                $status->insert(['msg' => '发表了一个新的动态', 'uid' => $person['Id'], 'time' => date('Y-m-d H:i:s', time())]);
                return json(['status' => 'success']);
            } else {
                return json(['status' => 'error']);
            }
        } else {
            return json(['status' => 'error']);
        }
    }

    public function add_comment()   //  评论发表接口
    {
        $cookie = Cookie::get('safecode');
        $status = new StatusData();
        $person = User::getBySafecode($cookie);
        if (input('post.')) {
            $post_data['name'] = $person['name'];
            $post_data['qq'] = $person['qq'];
            $post_data['info'] = nl2br(input('post.info'));
            $post_data['blog_Id'] = input('post.blog_Id');
            $comment = new Comment($post_data);
            if ($comment->save()) {
                $status->insert(['msg' => '发表了新的评论', 'uid' => $person['Id']]);
                return json(['status' => 'success']);
            } else {
                return json(['status' => 'error']);
            }
        }
    }

    public function delete_blog()   //删除动态
    {
        $cookie = Cookie::get('safecode');
        $person = User::getBySafecode($cookie);
        if (input('post.')) {
            $blog_Id = input('post.blog_Id');
            $blog_data = Blog::getById($blog_Id)->toArray();
            if ($blog_data['name'] == $person['name']) {
                if (Blog::destroy($blog_Id)) {
                    return json(['status' => 'success']);
                } else {
                    return json(['status' => 'error', 'msg' => '删除失败']);
                }
            } else {
                return json(['status' => 'error', 'msg' => '删除失败，不是你的动态']);
            }

        }
    }

    public function delete_comment()   //删除评论
    {
        $cookie = Cookie::get('safecode');
        $person = User::getBySafecode($cookie);
        if (input('post.')) {
            $comment_Id = input('post.comment_Id');
            $comment_data = Comment::getById($comment_Id)->toArray();
            if ($comment_data['name'] == $person['name']) {
                if (Comment::destroy($comment_data)) {
                    return json(['status' => 'success']);
                } else {
                    return json(['status' => 'error', 'msg' => '删除失败']);
                }
            } else {
                return json(['status' => 'error', 'msg' => '删除失败，不是你的评论']);
            }

        }
    }

    public function zan_comment()   //赞评论
    {
        if (input('post.')) {
            $comment_Id = input('post.comment_Id');
            if (Comment::where('Id', $comment_Id)->setInc('zan', 1)) {
                return json(['status' => 'success']);
            } else {
                return json(['status' => 'error']);
            }
        }
    }

    public function zan_blog()   //赞动态
    {
        if (input('post.')) {
            $blog_Id = input('post.blog_Id');
            if (Blog::where('Id', $blog_Id)->setInc('zan', 1)) {
                return json(['status' => 'success']);
            } else {
                return json(['status' => 'error']);
            }
        }
    }
}